/**
 * 
 */
package se.chalmers.spexet.booking.model;
import se.chalmers.spexet.booking.common.*;
import java.util.*;
/**
 * @author Winquman
 *
 */
public class SalesPeriod {
	private ArrayList<Order> sales;
	private Calendar start, end, control;
	private User endUser, controlUser;
	private final int periodID;
	private boolean finalized;
	
	public SalesPeriod(int id, Calendar startTime) {
		start = startTime;
		periodID = id;
		finalized = false;
	}
	
	public void finalize(User closedByUser) throws InvalidOrderModificationException{
		if(finalized)
			throw new InvalidOrderModificationException("Tried to close sales period " + this + 
					", but it was already closed by " + endUser + " at " + end);
		endUser = closedByUser;
		finalized = true;
		end = GregorianCalendar.getInstance();
	}
	
	public void add(Order o) throws InvalidOrderModificationException {
		if(finalized)
			throw new InvalidOrderModificationException("Tried to add order " + o + 
					" to finalized sales period " + this);
		if(!o.isFinalized())
			throw new InvalidOrderModificationException("Tried to add order " + o +
					" to sales period " + this + ", but the order is not finalized!");
		sales.add(o);
	}
}
